package control

import (
	"strings"
	"time"

	"github.com/xlvector/dlog"
)

func (p *Worker) persistPXWithRedis(px string) {
}

func (p *Worker) persistPXWithSSDB(px string) {
}

func (p *Worker) persistPXWithMongodb(px string) {
}

func GetTypeByPx(px string) string {
	pos := strings.Index(px, "://")
	if pos <= 0 {
		return ""
	}
	//dlog.Info("scan found: %s", px)

	return px[:pos]

}

func (p *Worker) persistPX(idx int) {
	//defer p.wg.Done()

	mongodbDB := p.conf.Worker.SPX.MDB
	mongodbCollect := p.conf.Worker.SPX.MCollect

	httpKeys := p.conf.Worker.SPX.HTTP
	httpsKeys := p.conf.Worker.SPX.HTTPS

	for {
		px, ok := <-p.pxch
		if !ok {
			dlog.Warn("px ch exit! %d", idx)
			return
		}

		if p.model.SSDB != nil {
			t := GetTypeByPx(px)
			if len(t) <= 0 {
				dlog.Warn("not found px type! %v", px)
				continue
			}

			switch t {
			case "http", "HTTP":
				for _, key := range httpKeys.ZSetKeys {
					err := p.model.SSDB.Zset(key, px, time.Now().Unix())
					if err != nil {
						dlog.Warn("persist ssdb fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					dlog.Info("ssdb save succ. key:%s px:%s", key, px)
				}
			case "https", "HTTPS":
				for _, key := range httpsKeys.ZSetKeys {
					err := p.model.SSDB.Zset(key, px, time.Now().Unix())
					if err != nil {
						dlog.Warn("persist ssdb fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					dlog.Info("ssdb save succ. key:%s px:%s", key, px)
				}
			case "socks4", "SOCKS4":
			case "socks5", "SOCKS5":
			}
		}

		if p.model.Redis != nil {
			t := GetTypeByPx(px)
			if len(t) <= 0 {
				dlog.Warn("not found px type! %v", px)
				continue
			}

			switch t {
			case "http", "HTTP":
				for _, key := range httpKeys.ZSetKeys {
					err := p.model.Redis.ZAdd(key, px, time.Now().Unix())
					if err != nil {
						p.statInc(StatHTTPZSetFail)
						dlog.Warn("persist redis fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					p.statInc(StatHTTPZSetSucc)
					dlog.Info("redis save succ. key:%s px:%s", key, px)
				}
				for _, key := range httpKeys.ListKeys {
					_, err := p.model.Redis.LPush(key, px)
					if err != nil {
						p.statInc(StatHTTPListFail)
						dlog.Warn("persist redis fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					p.statInc(StatHTTPListSucc)
					dlog.Info("reids save succ. key:%s px:%s", key, px)
				}
			case "https", "HTTPS":
				for _, key := range httpsKeys.ZSetKeys {
					err := p.model.Redis.ZAdd(key, px, time.Now().Unix())
					if err != nil {
						p.statInc(StatHTTPSZSetFail)
						dlog.Warn("persist redis fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					p.statInc(StatHTTPSZSetSucc)
					dlog.Info("redis save succ. key:%s px:%s", key, px)
				}
				for _, key := range httpsKeys.ListKeys {
					_, err := p.model.Redis.LPush(key, px)
					if err != nil {
						p.statInc(StatHTTPSListFail)
						dlog.Warn("persist redis fail! key:%s px:%s err:%v", key, px, err)
						continue
					}
					p.statInc(StatHTTPSListSucc)
					dlog.Info("redis save succ. key:%s px:%s", key, px)
				}
			case "socks4", "SOCKS4":
			case "socks5", "SOCKS5":
			}
		}

		if p.model.Mongodb != nil {
			rs := map[string]interface{}{
				"link":      px,
				"createdat": time.Now().Unix(),
			}
			err := p.model.Mongodb.SaveData2(mongodbDB, mongodbCollect, rs)
			if err != nil {
				dlog.Warn("persist mongodb fail! %v", err)
			} else {
				dlog.Warn("mongodb save succ!")
				continue
			}
		}

		dlog.Info("try all persit px: %v", px)
	}
}
